package com.controller.teacher;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;


import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.model.Teacher;


/**
 * 用于teacher4.jsp\teacher7.jsp\teacher8.jsp
 * 
 * @author he
 *
 */
public class StaController extends Controller {
	private String name;
	private String filename;
	private int page;
	private int rows;
	private String sort;
	private String order;
	private String id;
	private String URL;
	private String teacherName;
	private String msg;

	//获取审阅开题报告显示信息
	@ActionKey("/getInfo")
	public void index() {
		
		ArrayList<Object> array=(ArrayList<Object>) getSession().getAttribute("teacher");
		Teacher tch=(Teacher) array.get(0);

		
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher4");
		//操作的列+别名
		//学生姓名
		config.put("col_studentName", "student_name");
		config.put("alias_studentName", "StudentName");
		//论文名
		config.put("col_dissName", "thesistitle_name");
		config.put("alias_dissName", "dissName");
		//论文当前状态
		config.put("col_dissStateOfExamination", "kickoff_passtype");
		config.put("alias_dissStateOfExamination", "dissStateOfExamination");
		//论文id
		config.put("col_id", "kickoff_id");
		config.put("alias_id", "id");
		//教师姓名
		config.put("col_teacherName", "teacher_name");
		config.put("alias_teacherName", "teacherName");
		//论文存储地址
		config.put("col_dissOpenReportURL", "kickoff_report");
		config.put("alias_dissOpenReportURL", "dissOpenReportURL");
		//自定义命名结束
		config.put("col_teacherId", "teacher_id");
		config.put("value_teacherId",tch.get("teacher_id").toString());//此处为教师id，之后用session代替
		//教师匹配（id匹配学生）
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");
		name = getPara("name");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where 1=1";
		String select="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//---------------添加分页属性-----------------
		if (name != null)
			sql = sql + " and id like '%" + name + "%'";
		sql=sql+" and %s = %s";
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_teacherId"),config.get("value_teacherId"));
		select=String.format(select, 
				config.get("col_id"),config.get("alias_id"),
				config.get("col_studentName"),config.get("alias_studentName"),
				config.get("col_teacherName"),config.get("alias_teacherName"),
				config.get("col_dissName"),config.get("alias_dissName"),
				config.get("col_dissOpenReportURL"),config.get("alias_dissOpenReportURL"),
				config.get("col_dissStateOfExamination"),config.get("alias_dissStateOfExamination"));
		
		
		//----------------获取分页属性，传志前台-------------------
		Page<Record> stu=Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}

	//审阅开题报告与审核论文定稿需要用的文档下载
	@ActionKey("/downLoad")
	public void DownLoad() throws URISyntaxException, UnsupportedEncodingException {
		//------------获取论文存储相对地址---------------------
		//在tomcat服务器的doc目录下
		String tempURL = getPara("URL");
		URL=new String (tempURL.getBytes("iso-8859-1"),"utf-8");
		System.out.println(URL);
		
		//--------------获取论文存储绝对地址--------------------
		String path = this.getClass().getResource("/").toURI().getPath();
		path = path.substring(0, path.lastIndexOf("/WEB-INF"));
		URL=URL.replace(" ", "_");
		filename =URL;
		String type=getPara("type");
		if("4".equals(type))
			path = path + "/WEB-INF/uploadDir/student/04/2011/kickoff_report/" + URL;
		else if("8".equals(type))
			path = path + "/WEB-INF/uploadDir/student/04/2011/thesistemp_url/" + URL;
		else if("8_1".equals(type))
			path = path + "/WEB-INF/uploadDir/student/04/2011/ensuretemp_url/" + URL;
		else if("10".equals(type))
			path=path+"/WEB-INF/uploadDir/student/04/2011/fthesis_url/"+URL;
		System.out.println(path);
		//获取论文绝对地址结束

		//filename = new String(filename.getBytes("UTF-8"),"iso-8859-1");   
		
		HttpServletResponse res = this.getResponse();
		String agent = getRequest().getHeader("USER-AGENT");   
		if (null != agent && -1 != agent.indexOf("MSIE"))   //IE  
		{    
			filename = java.net.URLEncoder.encode(filename, "UTF-8");   
		}    
		  else if (null != agent && -1 != agent.indexOf("Mozilla")) //Firefox  
		{        
			  filename = new String(filename.getBytes("UTF-8"),"iso-8859-1");     
		}    
		else     
		{  
			filename = java.net.URLEncoder.encode(filename, "UTF-8");   
		}    
		try {
			//---------------论文下载----------------
			res.setContentType("application/x-download; charset=utf-8");
			res.setHeader("Content-disposition", "attachment; filename="+filename);
			InputStream in;
			in = new FileInputStream(path);
			byte buffer[] = new byte[1024];
			OutputStream out;
			out = res.getOutputStream();
			int len = -1;
			while ((len = in.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	//审核周进展中，设置不通过的老师留言
	@ActionKey("/setMessage")
	public void SetMessage() {
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher4");
		//操作的列+别名
		//开题报告意见
		config.put("col_message", "kickoff_tchmemo");
		//论文id
		config.put("col_id", "kickoff_id");
		
		
		
		//----------------获取前台传入信息---------------
		String Mmsg = getPara("teacherMessage");
		String Mid = getPara("id");
		
		
		
		//----------------基础sql语句----------------
		String sql="update %s set %s='%s' where %s=%s";
		
		//-------------------替换------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_message"),Mmsg,
				config.get("col_id"),Mid);
				
				
		Db.update(sql);
		renderJson("{\"success\":true}");
	}

	//开题报告通过与不通过
	@ActionKey("/openPass")
	public void openPass() {
		Map<String,String> config=new HashMap<String,String>();
		//--------数据库操作的表-----------------
		config.put("tableName", "he_teacher4");
		//操作的列+别名
		//论文状态
		config.put("col_dissStateOfExamination", "kickoff_passtype");
		//论文id
		config.put("col_id", "kickoff_id");
		
		//--------设置通过与不通过的信息------------
		config.put("pass", "C");
		config.put("noPass", "B");
		
		
		
		//-----------获取前台数据并转换------------
		String pass=getPara("pass");
		String Mmsg =null;
		if("yes".equals(pass))
			Mmsg = config.get("pass");
		else
			Mmsg=config.get("noPass");
		String Mid = getPara("id");
		
		
		//-----------------基础sql语句----------------
		String sql="update %s set %s='%s' where %s=%s";
		
		
		//------------------转换------------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_dissStateOfExamination"),Mmsg,
				config.get("col_id"),Mid);
		
		Db.update(sql);
		renderJson("{\"success\":true}");
	}
	
	
	//审核开题报告中，点击datagrid内的某一行，下面显示的信息发生改变
	@ActionKey("/selectStu")
	public void SelectStu() {
		//获取前台数据，并存入session中，然后将选中数据返回
		URL = getPara("URL");
		id=getPara("id");
		setSessionAttr("id", id);
		Map<String, Object> m=new HashMap<String, Object>();
		m.put("success", true);
		renderJson(m);
	}
	
	//获取周进展信息
	@ActionKey("/GetPOWT")
	public void GetPOWT(){
		
		ArrayList<Object> array=(ArrayList<Object>) getSession().getAttribute("teacher");
		Teacher tch=(Teacher) array.get(0);
		
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher7");
		//操作的列+别名
		//操作的列+别名
		//学生id
		config.put("col_StudentId", "student_no");
		config.put("alias_StudentId", "StudentId");
		//学生姓名
		config.put("col_StudentName", "student_name");
		config.put("alias_StudentName", "StudentName");
		//教师姓名
		config.put("col_teacherName", "teacher_name");
		config.put("alias_teacherName", "teacherName");
		//周进展提交时间
		config.put("col_POWTStudentSubmitMsgTime", "prorec_submittime");
		config.put("alias_POWTStudentSubmitMsgTime", "POWTStudentSubmitMsgTime");
		//周进展报告状态
//		config.put("col_POWTMsgState", "prorec_state");
//		config.put("alias_POWTMsgState", "POWTMsgState");
		//学生提交的周进展报告
		config.put("col_POWTmsg", "prorec_content");
		config.put("alias_POWTmsg", "POWTStudentMsg");
		//周进展报告id
		config.put("col_id", "prorec_id");
		config.put("alias_id", "id");
		//教师匹配（id匹配学生）
		config.put("col_teacherId", "teacher_id");
		config.put("value_teacherId", tch.get("teacher_id").toString());//此处为教师id，之后用session代替
		
		
		
		//---------------获取分页信息-------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");
		
		
		
		//--------------基础sql语句--------------
		String sql = " from %s where 1=1";
		String select ="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//------------轉換--------------------
		sql=String.format(sql, config.get("tableName"));
		select =String.format(select, 
				config.get("col_id"),config.get("alias_id"),
				config.get("col_StudentId"),config.get("alias_StudentId"),
				config.get("col_StudentName"),config.get("alias_StudentName"),
				config.get("col_teacherName"),config.get("alias_teacherName"),
				config.get("col_POWTStudentSubmitMsgTime"),config.get("alias_POWTStudentSubmitMsgTime"),
				//config.get("col_POWTMsgState"),config.get("alias_POWTMsgState"),
				config.get("col_POWTmsg"),config.get("alias_POWTmsg"));
		
		
		//-----------執行並返回數據給前臺-----------------
		if(name!=null)
			sql=sql+" and id like '%"+name+"%'";
		sql=sql+" and %s = %s";
		if (sort != null && order != null)
			sql = sql+" order by " + sort + " " + order;
		
		sql=String.format(sql, 
				config.get("col_teacherId"),config.get("value_teacherId"));
		Page<Record> result = Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", result.getTotalRow());
		m.put("rows", result.getList());
		renderJson(m);
		
	}
	
	
	//进展通过
	@ActionKey("/POWTPass")
	public void POWTPass(){
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher7");
		//操作的列
		//周进展报告状态
		config.put("col_POWTMsgState", "prorec_state");
		//周进展报告id
		config.put("col_id", "prorec_id");
		//教师回复时间
		config.put("col_tchReply", "prorec_time");
		//--------------设置通过与不通过的信息-----------
		config.put("pass", "C");
		
		
		//--------------获取通过的进展id-------------
		id=getPara("id");
		
		
		//------------基础sql语句-----------------
		String sql="update %s set %s='%s',%s='%s' where %s=%s";
		
		
		//------------获取当前时间--------------------
		Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置显示格式
		String nowTime="";
		nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示
		
		
		//------------转换----------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_POWTMsgState"),config.get("pass"),
				config.get("col_tchReply"),nowTime,
				config.get("col_id"),id);
		
		
		//------------执行并返回数据给前台-----------
		Db.update(sql);
		renderJson("{\"success\":true}");
	}
	
	
	
	//周进展不通过的理由
	@ActionKey("/POWTMsg")
	public void POWTMsg() throws UnsupportedEncodingException{
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher7");
		//操作的列+别名
		//周进展报告评语
		config.put("col_POWTTeacherGuidance", "prorec_reason");
		//周进展报告状态
		config.put("col_POWTMsgState", "prorec_state");
		//周进展报告id
		config.put("col_id", "prorec_id");
		//教师回复时间
		config.put("col_tchReply", "prorec_time");
		//------------设置通过不通过状态------------
		config.put("noPass", "B");
		
		
		
		//------------获取当前时间--------------------
		Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置显示格式
		String nowTime="";
		nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示
		
		
		//-----------获取前台传入数据-----------
		id=getPara("id");
		msg=StaController.ios2utf(getPara("msg"));
		
		
		if(id!=null)
		{
			//-----------基础sql语句----------------
			String sql="update %s set %s='%s',%s='%s',%s='%s' where %s=%s";
			
			//-----------转换----------------------
			sql=String.format(sql, config.get("tableName"),
					config.get("col_POWTTeacherGuidance"),msg,
					config.get("col_POWTMsgState"),config.get("noPass"),
					config.get("col_tchReply"),nowTime,
					config.get("col_id"),id);
			Db.update(sql);
			renderJson("{\"success\":true}");
		}
		else
		{
			renderJson("{\"success\":false}");
		}
	}

	//获取审阅论文定稿的信息
	@ActionKey("/getTEMDInfo")
	public void getTEMDInfo() {
		
		ArrayList<Object> array=(ArrayList<Object>) getSession().getAttribute("teacher");
		Teacher tch=(Teacher) array.get(0);
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher8");
		//操作的列+别名
		//论文名称
		config.put("col_dissName", "thesistitle_name");
		config.put("alias_dissName", "dissName");
		//论文类型
		config.put("col_dissType", "thesistitle_type");
		config.put("alias_dissType", "dissType");
		//论文来源
		config.put("col_dissSrc", "thesistitle_source");
		config.put("alias_dissSrc", "dissSrc");
		//学生id
		config.put("col_StudentId", "student_no");
		config.put("alias_StudentId", "StudentId");
		//学生姓名
		config.put("col_StudentName", "student_name");
		config.put("alias_StudentName", "StudentName");
		//定稿审核状态
		config.put("col_dissStateOfExamination", "initdraft_state");
		config.put("alias_dissStateOfExamination", "dissStateOfExamination");
		//定稿相对地址
		config.put("col_dissManuscriptURL", "thesistemp_url");
		config.put("alias_dissManuscriptURL", "dissManuscriptURL");
		//定稿id
		config.put("col_id", "stu_rela_thesis_id");
		config.put("alias_id", "id");
		//教师匹配（id匹配学生）
		config.put("col_teacherId", "teacher_id");
		config.put("value_teacherId", tch.get("teacher_id").toString());//此处为教师id，之后用session代替
		
		
		
		
		//--------------获取前台传入的分页对象------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");
		
		
		//-----------基础sql语句--------------------
		String sql=" from %s where 1=1";
		sql=sql+" and %s = %s";
		String select ="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//-----------转换-------------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_teacherId"),config.get("value_teacherId"));
		select =String.format(select, 
				config.get("col_dissName"),config.get("alias_dissName"),
				config.get("col_dissType"),config.get("alias_dissType"),
				config.get("col_dissSrc"),config.get("alias_dissSrc"),
				config.get("col_StudentId"),config.get("alias_StudentId"),
				config.get("col_StudentName"),config.get("alias_StudentName"),
				config.get("col_dissStateOfExamination"),config.get("alias_dissStateOfExamination"),
				config.get("col_dissManuscriptURL"),config.get("alias_dissManuscriptURL"),
				config.get("col_id"),config.get("alias_id"));
		
		
		
		//---------------执行并返回数据给前台--------------------
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		Page<Record> stu=Db.paginate(page, rows, select , sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
	@ActionKey("/getTEMDInfo_1")
	public void getTEMDInfo_1() {
		
		ArrayList<Object> array=(ArrayList<Object>) getSession().getAttribute("teacher");
		Teacher tch=(Teacher) array.get(0);
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher8_1");
		//操作的列+别名
		//论文名称
		config.put("col_dissName", "thesistitle_name");
		config.put("alias_dissName", "dissName");
		//论文类型
		config.put("col_dissType", "thesistitle_type");
		config.put("alias_dissType", "dissType");
		//论文来源
		config.put("col_dissSrc", "thesistitle_source");
		config.put("alias_dissSrc", "dissSrc");
		//学生id
//		config.put("col_StudentId", "stu_rela_thesis_id");
//		config.put("alias_StudentId", "StudentId");
		//学生姓名
		config.put("col_StudentName", "student_name");
		config.put("alias_StudentName", "StudentName");
		//定稿审核状态
		config.put("col_dissStateOfExamination", "ensuredraft_tchconfirm1");
		config.put("alias_dissStateOfExamination", "dissStateOfExamination");
		//定稿相对地址
		config.put("col_dissManuscriptURL", "ensuredraft_url");
		config.put("alias_dissManuscriptURL", "dissManuscriptURL");
		//定稿id
		config.put("col_id", "stu_rela_thesis_id");
		config.put("alias_id", "id");
		//教师匹配（id匹配学生）
		config.put("col_teacherId", "teacher_id");
		config.put("value_teacherId", tch.get("teacher_id").toString());//此处为教师id，之后用session代替
		
		
		
		
		//--------------获取前台传入的分页对象------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");
		
		
		//-----------基础sql语句--------------------
		String sql=" from %s where 1=1";
		sql=sql+" and %s = %s";
		String select ="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//-----------转换-------------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_teacherId"),config.get("value_teacherId"));
		select =String.format(select, 
				config.get("col_dissName"),config.get("alias_dissName"),
				config.get("col_dissType"),config.get("alias_dissType"),
				config.get("col_dissSrc"),config.get("alias_dissSrc"),
//				config.get("col_StudentId"),config.get("alias_StudentId"),
				config.get("col_StudentName"),config.get("alias_StudentName"),
				config.get("col_dissStateOfExamination"),config.get("alias_dissStateOfExamination"),
				config.get("col_dissManuscriptURL"),config.get("alias_dissManuscriptURL"),
				config.get("col_id"),config.get("alias_id"));
		
		
		
		//---------------执行并返回数据给前台--------------------
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		Page<Record> stu=Db.paginate(page, rows, select , sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
	
	//审核论文定稿通过
	@ActionKey("/FDPass")
	public void FDPass()
	{
		Map<String,String> config=new HashMap<String,String>();
		
		String end=getPara("end");
		//--------------设置通过不通过----------------
		config.put("pass", "B");
				
				
		//---------------获取前台传入id------------
		id=getPara("id");
		String sql=null;
		
		//--------------数据库操作的表----------------
		if(end==null)
		{
		config.put("tableName", "thesismodifytemp");
		config.put("col_FDdissState", "thesistemp_state");
		config.put("col_FDdissId", "thesistemp_student");
		config.put("col_sub", "thesistemp_isstusubmit");
		config.put("val_sub", "B");
		
		sql="UPDATE %s SET %s = '%s' WHERE (%s = '%s' and %s='%s')or( %s = '%s' and %s='%s')";
		sql=String.format(sql, config.get("tableName"),
				config.get("col_FDdissState"),"C",
				config.get("col_FDdissId"),id,
				config.get("col_sub"),config.get("val_sub"),
				config.get("col_FDdissId"),id,
				config.get("col_sub"),"A"
				);
		}
		else
		{
			config.put("tableName", "ensuredraft");
			config.put("col_FDdissState", "ensuredraft_tchconfirm1");
			config.put("col_FDdissId", "ensuredraft_student");
			sql="UPDATE %s SET %s = '%s' WHERE %s = '%s'";
			sql=String.format(sql, config.get("tableName"),
					config.get("col_FDdissState"),config.get("pass"),
					config.get("col_FDdissId"),id);
		}
		
		
		
		
		
		Db.update(sql);
		renderJson("{\"success\":true}");
	}
	
	
		//审核论文定稿不通过
		@ActionKey("/FDSetMsg")
		public void FDSetMsg() throws UnsupportedEncodingException
		{
			
			Map<String,String> config=new HashMap<String,String>();
			String end=getPara("end");
			
			//--------------数据库操作的表----------------
			if(end==null)
			{
				//--------------数据库操作的表----------------
				config.put("tableName", "thesismodifytemp");
				config.put("col_FDdissState", "thesistemp_state");
				config.put("col_FDdissId", "thesistemp_student");
				config.put("col_FDdissShortComment", "thesistemp_backcomment");
				config.put("col_sub", "thesistemp_isstusubmit");
				//--------------设置通过不通过----------------
				config.put("noPass", "B");
				
				
				//---------------获取前台数据---------------
				id=getPara("id");
				msg=StaController.ios2utf(getPara("msg"));
				
				//---------------基础sql语句---------------
				String sql="update %s set %s='%s',%s='%s' where (%s='%s' and %s='%s') or (%s='%s' and %s='%s')";
				
				
				//---------------转换--------------------
				sql=String.format(sql, config.get("tableName"),
						config.get("col_FDdissState"),config.get("noPass"),
						config.get("col_FDdissShortComment"),msg,
						config.get("col_FDdissId"),id,
						config.get("col_sub"),"A",
						config.get("col_FDdissId"),id,
						config.get("col_sub"),"B");
				Db.update(sql);
			}
			else
			{
				config.put("tableName", "ensuredraft");
				config.put("col_FDdissState", "ensuredraft_tchconfirm1");
				config.put("col_FDdissId", "ensuredraft_student");
				//--------------设置通过不通过----------------
				config.put("noPass", "C");
				
				
				//---------------获取前台数据---------------
				id=getPara("id");
				msg=StaController.ios2utf(getPara("msg"));
				
				//---------------基础sql语句---------------
				String sql="update %s set %s='%s' where %s=%s";
				
				
				//---------------转换--------------------
				sql=String.format(sql, config.get("tableName"),
						config.get("col_FDdissState"),config.get("noPass"),
						config.get("col_FDdissId"),id);
				Db.update(sql);
			}
			
			
			
			
			
			renderJson("{\"success\":true}");
		}
	
	//上传老师批改的定稿
	@ActionKey("/FDRetrunRevision")
	public void FDRetrunRevision() throws IOException, ServletException
	{
		String s=getClass().getResource("/").getFile().toString();
		s=s.substring(0, s.lastIndexOf("/WEB-INF"));
		s=URLDecoder.decode(s,"utf-8");
		
		
		//UploadFile uf=getFile("filename");
		UploadFile uf=getFile("filename", s+"/WEB-INF/uploadDir/teacher/04/2011/teacher_thesistemp_url/");
		System.out.println(uf.getFile().getPath());
		redirect("/pages1/finalT8.jsp");
		
		
		String id=getPara("hiddenId");
		
		/*Map<String,String> config=new HashMap<String,String>();
		
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher8");
		config.put("col_FDRetrun", "thesistemp_url");
		
		config.put("col_stu", "thesistemp_student");config.put("val_stu", id);//thesistemp_url
		config.put("col_state", "thesistemp_state");config.put("val_state", "A");//thesistemp_url
		config.put("col_newcomment", "thesistemp_newcomment");config.put("val_newcomment", "B");//thesistemp_url
		config.put("col_isstusubmit", "thesistemp_isstusubmit");config.put("val_isstusubmit", "A");//thesistemp_url
		config.put("col_url", "thesistemp_url");config.put("val_url", uf.getFileName());//thesistemp_url
		config.put("col_submittime", "thesistemp_submittime");config.put("val_submittime", new Date().toString());//thesistemp_url
		
		
		
		//---------------基础sql语句---------------
		//String sql="update %s set %s='%s' where %s=%s";
		//String sql="update he_teacher8 set thesistemp_url='cs' where initdraft_id='s2'";
		
		
		
		String sql="insert into %s(%s,%s,%s,%s,%s,%s) values(%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE %s='%s',%s='%s',%s='%s";
		
		
		//---------------转换--------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_FDRetrun"),config.get("col_FDRetrunValue"),
				config.get("col_FDId"),id
				);
		
		
		Db.update(sql);*/
		
		
		
		
		String sql="select * from thesismodifytemp where thesistemp_student=? and thesistemp_isstusubmit=?";//论文初稿修改临时表
		Boolean bool = false;
		Record thesismodifytemp = Db.findFirst(sql,id,"B");
		if(thesismodifytemp!=null)//初稿修改临时有记录的情况
		{
			
			System.out.println("----------------初稿修改临时有记录的情况-------------------");
				thesismodifytemp.set("thesistemp_state","A").set("thesistemp_newcomment","B").set("thesistemp_url",uf.getFileName()).set("thesistemp_submittime",new Date());
				if(Db.update("thesismodifytemp","thesistemp_id",thesismodifytemp))
					{
					bool=true;
					System.out.println("thesismodifytemp更新成功");
					}
				else bool=false;
		}
		if(thesismodifytemp==null)//初稿修改临时记录为空的情况
		{
			System.out.println("----------------初稿修改临时记录为空的情况-------------------");
			thesismodifytemp=new Record().set("thesistemp_student",id).set("thesistemp_state","A").set("thesistemp_newcomment","B").set("thesistemp_isstusubmit","B").set("thesistemp_url",uf.getFileName()).set("thesistemp_submittime",new Date());
		    if(Db.save("thesismodifytemp",thesismodifytemp))
		    	{
		    	bool=true;
		    	System.out.println("thesismodifytemp----id1"+id);
		    	System.out.println("thesismodifytemp插入成功");
		    	}
		    else bool=false;
		}
	}
	
	
	@ActionKey("/getEnsureInfo")
	public void getEnsureInfo()
	{
		
		ArrayList<Object> array=(ArrayList<Object>) getSession().getAttribute("teacher");
		Teacher tch=(Teacher) array.get(0);
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher9");
		//操作的列+别名
		//学生姓名
		config.put("col_studentName", "student_name");
		config.put("alias_studentName", "StudentName");
		//学生学号
		config.put("col_studentNo", "student_no");
		config.put("alias_studentNo", "StudentNo");
		//论文名
		config.put("col_thesisName", "thesistitle_name");
		config.put("alias_thesisName", "thesisName");
		//论文当前状态
		config.put("col_thesistitleCheckstate", "thesistitle_checkstate");
		config.put("alias_thesistitleCheckstate", "CheckState");
		//查重百分比
		config.put("col_ensurePercent", "thesistitle_ensurepercent");
		config.put("alias_ensurePercent", "ensurePercent");
		//id
		config.put("col_thesistitleId", "thesistitle_id");
		config.put("alias_thesistitleId", "id");
		//教师匹配（id匹配学生）
		config.put("col_teacherId", "teacher_id");
		config.put("value_teacherId", tch.get("teacher_id").toString());//此处为教师id，之后用session代替
		
		
		
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");
		name = getPara("name");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where 1=1";
		String select="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//---------------添加分页属性-----------------
		if (name != null)
			sql = sql + " and id like '%" + name + "%'";
		sql=sql+" and %s = %s";
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_teacherId"),config.get("value_teacherId"));
		select=String.format(select, 
				config.get("col_thesistitleId"),config.get("alias_thesistitleId"),
				config.get("col_studentName"),config.get("alias_studentName"),
				config.get("col_ensurePercent"),config.get("alias_ensurePercent"),
				config.get("col_thesistitleCheckstate"),config.get("alias_thesistitleCheckstate"),
				config.get("col_thesisName"),config.get("alias_thesisName"),
				config.get("col_studentNo"),config.get("alias_studentNo"));
		
		
		//----------------获取分页属性，传志前台-------------------
		Page<Record> stu=Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
	
	
	
	@ActionKey("/getFinalInfo")
	public void getFinalInfo(){
		
		ArrayList<Object> array=(ArrayList<Object>) getSession().getAttribute("teacher");
		Teacher tch=(Teacher) array.get(0);
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher10");
		//操作的列+别名
		//学生姓名
		config.put("col_studentName", "student_name");
		config.put("alias_studentName", "StudentName");
		//学生学号
		config.put("col_studentNo", "student_no");
		config.put("alias_studentNo", "StudentNo");
		//论文名
		config.put("col_thesisName", "thesistitle_name");
		config.put("alias_thesisName", "thesisName");
		//查重百分比
		config.put("col_ensurePercent", "thesistitle_ensurepercent");
		config.put("alias_ensurePercent", "ensurePercent");
		//提交时间
		config.put("col_sbmTime", "fthesis_submittime");
		config.put("alias_sbmTime", "submittime");
		//提交时间
		config.put("col_URL", "fthesis_url");
		config.put("alias_URL", "thesisURL");
		//id
		config.put("col_thesistitleId", "thesistitle_id");
		config.put("alias_thesistitleId", "id");
		//学生id，用于压入final
		config.put("col_stuId", "stu_rela_thesis_id");
		config.put("alias_stuId", "stuId");
		//当前论文状态（是否确定为最终论文）
		config.put("col_sta", "fthesis_confirm");
		config.put("alias_sta", "sta");
		//教师匹配（id匹配学生）
		config.put("col_teacherId", "teacher_id");
		config.put("value_teacherId", tch.get("teacher_id").toString());//此处为教师id，之后用session代替
		
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");
		name = getPara("name");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where 1=1";
		String select="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//---------------添加分页属性-----------------
		if (name != null)
			sql = sql + " and id like '%" + name + "%'";
		sql=sql+" and %s = %s";
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_teacherId"),config.get("value_teacherId"));
		select=String.format(select, 
				config.get("col_thesistitleId"),config.get("alias_thesistitleId"),
				config.get("col_studentName"),config.get("alias_studentName"),
				config.get("col_ensurePercent"),config.get("alias_ensurePercent"),
				config.get("col_thesisName"),config.get("alias_thesisName"),
				config.get("col_URL"),config.get("alias_URL"),
				config.get("col_sbmTime"),config.get("alias_sbmTime"),
				config.get("col_studentNo"),config.get("alias_studentNo"),
				config.get("col_stuId"),config.get("alias_stuId"),
				config.get("col_sta"),config.get("alias_sta"));
		
		
		//----------------获取分页属性，传志前台-------------------
		Page<Record> stu=Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
	
	@ActionKey("/finalPass")
	public void finalPass() throws UnsupportedEncodingException
	{
		String stuId=getPara("stuId");
		String comment=StaController.ios2utf(getPara("comment"));
		String score=StaController.ios2utf(getPara("score"));
		
		
		System.out.println(stuId);
		System.out.println(comment);
		System.out.println(score);
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "finalthesis");
		//学生id
		config.put("col_finalStu", "fthesis_student");
		config.put("value_finalStu", stuId);
		//教师评价
		config.put("col_ftchcom", "fthesis_guidetchcomment");
		config.put("value_ftchcom", comment);
		//论文评分
		config.put("col_fsco", "fthesis_guidtchescore");
		config.put("value_fsco", score);
		//确定终稿时间
		config.put("col_submittime", "fthesis_submittime");
		config.put("value_submittime", StaController.getNowTime());
		//导师确认终稿
		config.put("col_fthesisConfirm", "fthesis_confirm");
		config.put("value_fthesisConfirm", "B");
		
		
		
		String sql="update %s set %s='%s',%s='%s',%s='%s' where %s='%s'";
		
		
		if(confirmFinal(stuId)!=true)
		{
			renderJson("{\"success\":false}");
			return;
		}
		
		

		
		
		//String sql="insert into %s(%s,%s,%s,%s,%s,%s) values ('%s','%s','%s','%s','%s','%s')";
		
		
		
		//----------------替换----------------------
		/*sql=String.format(sql, 
				config.get("tableName"),
				config.get("col_finalStu"),config.get("col_tesUrl"),
				config.get("col_ftchcom"),config.get("col_fsco"),
				config.get("col_submittime"),config.get("col_fthesisConfirm"),
				config.get("value_finalStu"),config.get("value_tesUrl"),
				config.get("value_ftchcom"),config.get("value_fsco"),
				config.get("value_submittime"),config.get("value_fthesisConfirm"));
		*/
		sql=String.format(sql, 
				config.get("tableName"),
				config.get("col_ftchcom"),config.get("value_ftchcom"),
				config.get("col_fsco"),config.get("value_fsco"),
				config.get("col_fthesisConfirm"),config.get("value_fthesisConfirm"),
				config.get("col_finalStu"),config.get("value_finalStu"));
		
		//----------------获取分页属性，传志前台-------------------
		Db.update(sql);
		renderJson("{\"success\":true}");
	}
	public boolean confirmFinal(String stuId)
	{
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher10");
		//学生id
		config.put("col_stuId", "stu_rela_thesis_id");
		config.put("value_stuId", stuId);
		//论文地址
		config.put("col_checkstate", "thesistitle_checkstate");
		config.put("value_checkstate", "FF");
		
		String sql="update %s set %s='%s' where %s='%s'";
		
		
		sql=String.format(sql, 
				config.get("tableName"),
				config.get("col_checkstate"),config.get("value_checkstate"),
				config.get("col_stuId"),config.get("value_stuId"));
		Db.update(sql);
		return true;
	}
	
	
	
	@ActionKey("/getCheckReplyInfo")
	public void getCheckReplyInfo()
	{
		
		ArrayList<Object> array=(ArrayList<Object>) getSession().getAttribute("teacher");
		Teacher tch=(Teacher) array.get(0);
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher11");
		//操作的列+别名
		//学生姓名
		config.put("col_studentName", "student_name");
		config.put("alias_studentName", "StudentName");
		//学生学号
		config.put("col_studentNo", "student_no");
		config.put("alias_studentNo", "StudentNo");
		//学生主键
		config.put("col_studentId", "student_id");
		config.put("alias_studentId", "StudentId");
		//论文名
		config.put("col_thesisName", "thesistitle_name");
		config.put("alias_thesisName", "thesisName");
		//答辩组名
		config.put("col_replygrpName", "replygrp_name");
		config.put("alias_replygrpName", "replygrpName");
		//答辩组组长
		config.put("col_replygrpHeadName", "replygrp_headname");
		config.put("alias_replygrpHeadName", "replygrpHeadName");
		//答辩组秘书
		config.put("col_replygrpSecretaryName", "replygrp_secretaryname");
		config.put("alias_replygrpSecretaryName", "replygrpSecretaryName");
		//答辩地点
		config.put("col_replygrpPlace", "replygrp_place");
		config.put("alias_replygrpPlace", "replygrpPlace");
		//答辩时间
		config.put("col_replygrpTime", "replygrp_time");
		config.put("alias_replygrpTime", "replygrpTime");
		//教师匹配（id匹配学生）
		config.put("col_teacherId", "teacher_id");
		config.put("value_teacherId", tch.get("teacher_id").toString());//此处为教师id，之后用session代替
		
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");
		name = getPara("name");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where 1=1";
		String select="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//---------------添加分页属性-----------------
		if (name != null)
			sql = sql + " and id like '%" + name + "%'";
		sql=sql+" and %s = %s";
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_teacherId"),config.get("value_teacherId"));
		select=String.format(select, 
				config.get("col_studentName"),config.get("alias_studentName"),
				config.get("col_studentNo"),config.get("alias_studentNo"),
				config.get("col_thesisName"),config.get("alias_thesisName"),
				config.get("col_studentId"),config.get("alias_studentId"),
				config.get("col_replygrpName"),config.get("alias_replygrpName"),
				config.get("col_replygrpHeadName"),config.get("alias_replygrpHeadName"),
				config.get("col_replygrpSecretaryName"),config.get("alias_replygrpSecretaryName"),
				config.get("col_replygrpPlace"),config.get("alias_replygrpPlace"),
				config.get("col_replygrpTime"),config.get("alias_replygrpTime"));
		
		
		//----------------获取分页属性，传志前台-------------------
		Page<Record> stu=Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
	
	
	@ActionKey("/getCheckReplyInfo_1")
	public void getCheckReplyInfo_1()
	{
		
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher11_1");
		//操作的列+别名
		//学生姓名
		config.put("col_tchName", "teacher_name");
		config.put("alias_tchName", "tchName");
		//学生主键
		config.put("col_studentId", "student_id");
		
		
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		String id=getPara("stuId");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where 1=1";
		String select="select %s as %s";
		
		
		//---------------添加分页属性-----------------
		sql=sql+" and %s = %s";
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_studentId"),id);
		select=String.format(select, 
				config.get("col_tchName"),config.get("alias_tchName"));
		
		
		//----------------获取分页属性，传志前台-------------------
		sql=select+sql;
		String result="";
		List<String> name= Db.query(sql);
		for(int i=0;i<name.size()-1;i++)
		{
			result=result+name.get(i)+",";
		}
		result=result+name.get(name.size()-1);
		result="组员:"+result;
		renderJson(result);
	}
	
	
	
	@ActionKey("/getScoreInfo")
	public void getScoreInfo()
	{
		
		ArrayList<Object> array=(ArrayList<Object>) getSession().getAttribute("teacher");
		Teacher tch=(Teacher) array.get(0);
		
		Map<String,String> config=new HashMap<String,String>();
		//--------------数据库操作的表----------------
		config.put("tableName", "he_teacher12");
		//操作的列+别名
		//学生姓名
		config.put("col_studentName", "student_name");
		config.put("alias_studentName", "StudentName");
		//学生学号
		config.put("col_studentNo", "student_no");
		config.put("alias_studentNo", "StudentNo");
		//论文名
		config.put("col_thesisName", "thesistitle_name");
		config.put("alias_thesisName", "thesisName");
		//最终论文地址
		config.put("col_fthesisUrl", "fthesis_url");
		config.put("alias_fthesisUrl", "fthesisUrl");
		//指导教师评分
		config.put("col_fthesisGuidetchcomment", "fthesis_guidetchcomment");
		config.put("alias_fthesisGuidetchcomment", "fthesisGuidetchcomment");
		//评阅评分
		config.put("col_fthesisJudgetchcomment", "fthesis_judgetchcomment");
		config.put("alias_fthesisJudgetchcomment", "fthesisJudgetchcomment");
		//
		config.put("col_replyrecordMemo", "replyrecord_memo");
		config.put("alias_replyrecordMemo", "replyrecordMemo");
		//论文评分
		config.put("col_fthesisGuidtchescore", "fthesis_guidtchescore");
		config.put("alias_fthesisGuidtchescore", "fthesisGuidtchescore");
		//论文评分
		config.put("col_fthesisJudgetchscore", "fthesis_judgetchscore");
		config.put("alias_fthesisJudgetchscore", "fthesisJudgetchscore");
		//论文评分
		config.put("col_replyrecordScore", "replyrecord_score");
		config.put("alias_replyrecordScore", "replyrecordScore");
		//教师匹配（id匹配学生）
		config.put("col_teacherId", "teacher_id");
		config.put("value_teacherId", tch.get("teacher_id").toString());//此处为教师id，之后用session代替
		
		
		
		
		//-----------获取前台传过来的参数，用于分页获取数据--------------
		page = getParaToInt("page");
		rows = getParaToInt("rows");
		sort = getPara("sort");
		order = getPara("order");
		name = getPara("name");

		
		
		//--------------基础sql语句,使用占位符占位，在之后再替换-------------
		String sql = " from %s where 1=1";
		String select="select %s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s,%s as %s";
		
		
		//---------------添加分页属性-----------------
		if (name != null)
			sql = sql + " and id like '%" + name + "%'";
		sql=sql+" and %s = %s";
		if (sort != null && order != null)
			sql = sql + " order by " + sort + " " + order;
		
		
		//----------------替换----------------------
		sql=String.format(sql, config.get("tableName"),
				config.get("col_teacherId"),config.get("value_teacherId"));
		select=String.format(select, 
				config.get("col_studentName"),config.get("alias_studentName"),
				config.get("col_studentNo"),config.get("alias_studentNo"),
				config.get("col_thesisName"),config.get("alias_thesisName"),
				config.get("col_fthesisUrl"),config.get("alias_fthesisUrl"),
				config.get("col_fthesisGuidetchcomment"),config.get("alias_fthesisGuidetchcomment"),
				config.get("col_fthesisJudgetchcomment"),config.get("alias_fthesisJudgetchcomment"),
				config.get("col_replyrecordMemo"),config.get("alias_replyrecordMemo"),
				config.get("col_fthesisGuidtchescore"),config.get("alias_fthesisGuidtchescore"),
				config.get("col_fthesisJudgetchscore"),config.get("alias_fthesisJudgetchscore"),
				config.get("col_replyrecordScore"),config.get("alias_replyrecordScore"));
		
		
		//----------------获取分页属性，传志前台-------------------
		Page<Record> stu=Db.paginate(page, rows, select, sql);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", stu.getTotalRow());
		m.put("rows", stu.getList());
		renderJson(m);
	}
	
	
	
	
	/**
	 * 格式化标准时间
	 * 
	 */
	public static String getDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(new Date());
	}
	/**
	 * 转码
	 * @param s
	 * @throws UnsupportedEncodingException
	 */
	public static String ios2utf(String s) throws UnsupportedEncodingException
	{
		if(s==null)
			return null;
		return new String(s.getBytes("iso8859-1"),"utf-8");
	}
	/**
	 * 获取当前时间
	 * 
	 */
	public static String getNowTime(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		return df.format(new Date());
	}
}
